<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleBuild"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
         
          v-hasPermi="['system:operationLog:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
        
          v-hasPermi="['system:operationLog:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
        
          v-hasPermi="['system:operationLog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="operationLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="数据级别" align="center" prop="dataLevel" :formatter="formatdataLevelCode"/>
      <el-table-column label="部门" align="center" prop="orgName" />
      <el-table-column label="添加人" align="center" prop="createUserName" />
      <el-table-column label="添加时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.dataLevel==='0'"
            @click="handleUpdate(scope.row)"
          
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
             v-if="scope.row.dataLevel==='0'"
            @click="handleDelete(scope.row)"
          
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />
     <!-- 添加或修改数据权限对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-row>
          <el-col :span="24">
            <el-form-item label="名称:" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="数据级别:" prop="dataLevel" label-width="100px">
                <el-radio v-model="form.dataLevel" label="0">自定义</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="24" >
            <el-form-item label="部门" prop="parentId">
              <treeselect
                v-model="form.orgIdList"
                multiple
                :options="treedata"
                node-key = 'id'
                :normalizer="normalizer"
                :show-count="true"
                placeholder="选择上级菜单"
              />

            </el-form-item>
          </el-col>
        </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { dataauthoritylist,remove,submit } from "@/api/system/dataAuthority";
import {getCompany} from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Post",
    components: { Treeselect },
  // dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      operationLogList: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        name: undefined,
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
       // 表单参数
      form: {},
            // 表单校验
      rules: {
      name:[{ required: true,pattern: '[^ \x20]+', trigger: 'blur', message: '请输入名称' }],
      dataLevel:[{ required: true, message: "请选择数据级别", trigger: "change" }],
      orgIdList:[{ required: true, message: "请选择部门", trigger: "change" }]
       
      },
      treedata:[]
    };
  },
  created() {
    this.getList();
     this.getDeptTreeFun()
  },
  methods: {
        /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
         id: node.id,
        label: node.companyName,
        children: node.children
      };
    },
    /** 查询操作列表 */
    getList() {
      this.loading = true;
      dataauthoritylist(this.queryParams).then(response => {
        this.operationLogList = response.body.records;
        this.total = response.body.total;
        this.loading = false;
      });
    },
formatdataLevelCode(row, column, cellValue) {
      const dataLevel = {
        0: '自定义',
        1: '本人',
        2: '本部门',
        3: '本部门及下属',
        5: '全部'
      };
      return dataLevel[cellValue];
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    getDeptTreeFun(){
         getCompany().then((res)=>{
        if(res.code == 200){
            this.treedata= res.body;
        }else{
           
        }
    })
    },
    //新建
    handleBuild (){
      this.title =  "新建";
      this.open = true;
      this.form={}
    },
    // 编辑
    handleUpdate(row){
      this.form={...row}
      this.open = true;
      this.title = "编辑";
    },
    // 删除
    handleDelete(row){
        const userIds = {
          ids:row.id || this.ids
        }
      this.$modal.confirm('是否确认删除这条的数据项？').then(function() {
        return remove(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});

    },
  /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            submit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
          this.form.createUserName= localStorage.getItem('username')
          this.form.createUser=  localStorage.getItem('userId')
            submit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    close(){
      this.open = false;
    }
  }
};
</script>
<style lang="less" scoped>
::v-deep .vue-treeselect__multi-value-item{
  background: rgb(243.9,244.2,244.8) !important;
    color: #909399 !important;
}
::v-deep .vue-treeselect__value-remove{
  color: #909399 !important;
       padding-left: 0px !important; 
    border-left: none !important;
}
</style>
